﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Ink;
using KSAWPFExtensions;
using KSASupport;
using KSASupport.BlobTracking;
using PhotoLoader;
using System.IO;

namespace KSAWPFDemo.InkCanvas
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class KSAInkCanvas : UserControl
    {
        InkAnalyzer ia;

        public KSAInkCanvas()
        {
            InitializeComponent();
            this.Loaded += KSAInkCanvas_Loaded;
            ia = new InkAnalyzer();
            
        }

        void KSAInkCanvas_Loaded(object sender, RoutedEventArgs e)
        {
            List<string> folders = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)).ToList();
            foreach (string folder in folders)
            {
                string[] files = Directory.GetFiles(folder,"*.jpg");
                foreach(string file in files)
                {
                    try
                    {
                        Image image = new Image();
                        image.Width = 280;
                        Loader.SetSource(image, file);
                        image.Tag = file;
                        ImageList.Items.Add(image);

                    }
                    catch (Exception)
                    { }
                }
            }
        }

        private readonly Dictionary<int, Stroke> _activeStrokes = new Dictionary<int, Stroke>();
        private readonly TouchColor _touchColor = new TouchColor();


        private void Canvas_TouchDown(object sender, TouchEventArgs e)
        {

            Stroke stroke;

            if (_activeStrokes.TryGetValue(e.TouchDevice.Id, out stroke))
            {
                FinishStroke(stroke);
                return;
            }
            

            // Create new stroke, add point and assign a color to it.
            Stroke newStroke = new Stroke();

            BlobSide side = ((KSAWPFTouchDevice)e.TouchDevice).Contact.Side;
            switch (side) //heh
            {
                case BlobSide.Bottom:
                    {
                        newStroke.Color = Colors.Blue;
                        break;
                    }
                case BlobSide.Right:
                    {
                        newStroke.Color = Colors.Red;
                        break;
                    }
                case BlobSide.Left:
                    {
                        newStroke.Color = Colors.Green;
                        break;
                    }
                case BlobSide.Top:
                    {
                        newStroke.Color = Colors.Orange;
                        break;
                    }
                default:
                    {
                        newStroke.Color = Colors.Black;
                        break;
                    }
            }
            

            //newStroke.Color = _touchColor.GetColor();
            newStroke.Id = e.TouchDevice.Id;

            // Add new stroke to the collection of strokes in drawing.
            _activeStrokes[newStroke.Id] = newStroke;

        }

        private void FinishStroke(Stroke stroke)
        {
            //Seal stroke for better performance
            stroke.Freeze();

            // Remove finished stroke from the collection of strokes in drawing.
            _activeStrokes.Remove(stroke.Id);
        }

        private void Canvas_TouchUp(object sender, TouchEventArgs e)
        {
            // Find the stroke in the collection of the strokes in drawing.
            Stroke stroke;
            if (_activeStrokes.TryGetValue(e.TouchDevice.Id, out stroke))
            {
                FinishStroke(stroke);
            }

        }

        private void Canvas_TouchMove(object sender, TouchEventArgs e)
        {
            Canvas drawingCanvas = sender as Canvas;
            if (drawingCanvas != null)
            {
                Stroke stroke;
                if (_activeStrokes.TryGetValue(e.TouchDevice.Id, out stroke))
                {
                    // Add contact point to the stroke
                    TouchPoint tp = e.GetTouchPoint(drawingCanvas);
                    stroke.Add(tp.Position);
                    stroke.AddToCanvas(drawingCanvas);
                }
            }
        }

        private void Canvas_TouchLeave(object sender, TouchEventArgs e)
        {
            // System.Diagnostics.Debug.WriteLine("Touch leave"); 
        }

        private void Canvas_TouchEnter(object sender, TouchEventArgs e)
        {
            //  System.Diagnostics.Debug.WriteLine("Enter"); 
        }

        /// <summary>
        /// If an image from the image list is touched, set it as the background
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ImageList_TouchDown_1(object sender, TouchEventArgs e)
        {
            // store off what is touched. we'll check the manipulation event later, and if its 'small', we'll use this image.
            Image item = e.TouchDevice.DirectlyOver as Image;
            ImageList.Tag = item;
        }


        private void LoadImageButton_PreviewTouchDown_1(object sender, TouchEventArgs e)
        {
            if (ImageList.Opacity == 1)
            {
                ImageList.Opacity = 0;
                LoadImageButton.Margin = new Thickness(0, 0, 100, 100);
            }
            else
            {
                ImageList.Opacity = 1;
                LoadImageButton.Margin = new Thickness(0,0,300,100);
            }
        }

        private void ImageList_ManipulationDelta_1(object sender, ManipulationDeltaEventArgs e)
        {
            if (e.DeltaManipulation.Translation.X < 5)
            {
                Image item = ((ListView)sender).Tag as Image;
                if (item != null)
                {
                    BitmapImage bmp = new BitmapImage(new Uri(item.Tag as string));
                    ImageBrush imageBrush = new ImageBrush(bmp);
                    imageBrush.Stretch = Stretch.UniformToFill;
                    BackgroundGrid.Background = imageBrush;

                }
            }
        }

        private void ImageList_ManipulationCompleted_1(object sender, ManipulationCompletedEventArgs e)
        {
            if (e.TotalManipulation.Translation.X < 5)
            {
                Image item = ((ListView)sender).Tag as Image;
                if (item != null)
                {
                    BitmapImage bmp = new BitmapImage(new Uri(item.Tag as string));
                    ImageBrush imageBrush = new ImageBrush(bmp);
                    imageBrush.Stretch = Stretch.UniformToFill;
                    BackgroundGrid.Background = imageBrush;

                }
            }
        }
    }
}
